<?php
/**
 * ArrowErrorHandler - onvert error to exception
* @access public
* @package ArrowErrorHandler
* @author 3code group
*/
class ArrowErrorHandler implements IErrorHandler {

	/**
	 * Object instance keeper
	 *
	 * @var oInstance
	 */
	private static $oInstance = null;

	/**
	 * Singleton
	 * 
	 * @return ArrowErrorHandler
	 */
	public static function getDefault(){
		if( self::$oInstance == null ){
			self::$oInstance = new ArrowErrorHandler();
		}
		return self::$oInstance;
	}
	
	public function setLevel( $level ) {
		$err = E_ALL ;
		switch( $level ) {
			case "E_ALL": $err = E_ALL; break;
			case "E_WARNING": $err = E_WARNING; break;
			case "E_NOTICE": $err = E_NOTICE; break;
			case "E_ERROR": $err = E_ERROR; break;
			default:
				throw new Exception( "Nieznany poziom logowania: $level" );
		}	
	
		error_reporting($err);
	}
	
    /**
    * Constructor
    */
    private function __construct( ) {
    	error_reporting( E_ALL );
		ini_set( "display_errors", 1 ) ;
        set_error_handler( array($this, "raiseError") );
    }

    /**
    * @param int $err_no
    * @param string $err_msg
    * @param string $err_file
    * @param int $err_line
    * @return void
    */
    public function raiseError($err_no, $err_msg, $err_file, $err_line) {
    	if( error_reporting() != 0 && ( error_reporting() & $err_no == $err_no ) /*&& $err_no != 2048*/ ){  //0 - jak jest @ to == 0 // 2048 - warning about timezone settings
    		if( $err_no != 2048 ) {
   				$exception = new ErrorException( serialize( array( "msg" => "Error: ".$err_msg ) ), 0, $err_no, $err_file, $err_line) ;
    			throw $exception;
    		} else {
    			$this->printErr( array( "msg" => $err_msg, "no" => $err_no, "file" => $err_file, "line" => $err_line ) );
   				exit;			 			
    		}
    	}
    	
		return true;
    }
    
    private function printErr( $arr ) {
    	$msg =
 <<<MSG
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Error Strict</title>
	<meta name="description" content="Error Strict" />
	<meta name="keywords" content="Error Strict" />
</head>
<body>
	<table border="0" >
		<tbody>
			<tr>
				<td>
					Error Number:
				</td>
				<td>
					{$arr["no"]}
				</td>
			</tr>
			<tr>
				<td>
					Message:
				</td>
				<td>
					{$arr["msg"]}
				</td>
			</tr>
			<tr>
				<td>
					File:
				</td>
				<td>
					{$arr["file"]}
				</td>
			</tr>
			<tr>
				<td>
					Line:
				</td>
				<td>
					{$arr["line"]}
				</td>
			</tr>
		</tbody>
	</table>
</body>
</html> 
MSG;
 		echo $msg ;
   
    }
}

?>